package com.itany.conf;

import org.jasig.cas.client.authentication.AuthenticationFilter;
import org.jasig.cas.client.session.SingleSignOutFilter;
import org.jasig.cas.client.session.SingleSignOutHttpSessionListener;
import org.jasig.cas.client.validation.Cas30ProxyReceivingTicketValidationFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.EventListener;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class CasClientConfig {

    private static  final  String CAS_SERVER_URL_PREFIX="http://localhost:8443/cas/";
    private static  final  String CAS_SERVER_URL_LOGIN="http://localhost:8443/cas/login";
    //本机名称
    private static  final  String SERVER_NAME="http://localhost:9003/";

    /**
     * 登录过滤
     * @return
     */
    @Bean
    public FilterRegistrationBean filterSingleRegistrat(){
        System.out.println("--------------filterSingleRegistrat------------------");
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(new SingleSignOutFilter());
        registrationBean.addUrlPatterns("/user/*");
        Map<String,String> init = new HashMap<String,String>();
        init.put("casServerUrlPrefix",CAS_SERVER_URL_PREFIX);
        registrationBean.setInitParameters(init);
        registrationBean.setOrder(1);
        return  registrationBean;
    }


    /**
     * 代理请求验证
     */
    @Bean
    public FilterRegistrationBean filterValidationRegistration(){
        System.out.println("--------------filterValidationRegistration------------------");
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(new Cas30ProxyReceivingTicketValidationFilter());
        registrationBean.addUrlPatterns("/user/*");
        Map<String,String> init = new HashMap<String,String>();
        init.put("casServerUrlPrefix",CAS_SERVER_URL_PREFIX);
        init.put("serverName",SERVER_NAME);
        init.put("useSession","true");//是否在会话中存储断言 false则每次请求都要票验
        registrationBean.setInitParameters(init);
        registrationBean.setOrder(1);
        return  registrationBean;

    }

    /**
     * 授权过滤
     */
    @Bean
    public FilterRegistrationBean filterAuthenticationRegistration(){
        System.out.println("--------------filterAuthenticationRegistration------------------");
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(new AuthenticationFilter());
        registrationBean.addUrlPatterns("/user/*");
        Map<String,String> init = new HashMap<String,String>();
        init.put("casServerLoginUrl",CAS_SERVER_URL_LOGIN);
        init.put("serverName",SERVER_NAME);
        init.put("ignorePattern","/user/userserver/casLogout");
        init.put("ignoreUrlPatternType","com.itany.conf.SimpleUrlPatterMatchStatey");
        registrationBean.setInitParameters(init);
        registrationBean.setOrder(1);
        return  registrationBean;

    }

    /**
     * 包装过滤器
     * @return
     */
    @Bean
    public FilterRegistrationBean filterWrapperRegistration(){
        System.out.println("--------------filterWrapperRegistration------------------");
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(new MyWrapperFilter());
        registrationBean.addUrlPatterns("/user/*");
        registrationBean.setOrder(1);
        return  registrationBean;
    }


    @Bean
    public ServletListenerRegistrationBean<EventListener> singleSignOutListenerRegistration(){
        System.out.println("--------------singleSignOutListenerRegistration------------------");
        ServletListenerRegistrationBean<EventListener> registrationBean = new ServletListenerRegistrationBean<EventListener>();
        registrationBean.setListener(new SingleSignOutHttpSessionListener());
        registrationBean.setOrder(1);
        return registrationBean;
    }



}
